package com.ziq.zcamp.dialog.share

import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ImageUtils
import com.gmlive.camp.dialog.share.*
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.ziq.base.base.BaseDialog
import com.ziq.base.extension.*
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.activities.ActivitiesInfo
import com.ziq.zcamp.bean.coupon.CouponInfo
import com.ziq.zcamp.bean.life.CampLifeInfo
import com.ziq.zcamp.bean.place.PlaceDetail
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.bean.product.PlaceProduct
import com.ziq.zcamp.databinding.DialogShareCreatePicBinding
import com.ziq.zcamp.databinding.ItemDialogShareCreatePictureActionBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.WXAccount


class ShareDialogPictureCreate(val activity: FragmentActivity, val source : String = "") : BaseDialog(activity) {

    private val binding: DialogShareCreatePicBinding by binding()
    private lateinit var shareActionAdapter: ActionAdapter
    private var sharePicCache: SharePicCacheInterface? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.decorView?.let {
            window?.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
            window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        }

        binding.root
        if (GlobalContextManager.getCurrentActivity() != null){
            binding.container.setPadding(0, BarUtils.getStatusBarHeight(), 0,0)
        }else{
            binding.container.setPadding(0, 50.dp, 0,0)
        }
        binding.back.click {
            dismiss()
        }

        setAnimateView(binding.root)

        shareActionAdapter = ActionAdapter()
        shareActionAdapter.setActionCallback {
            when (it) {
                GlobalContextManager.getString(R.string.share_wechat) -> {
                    sharePicCache?.let {
                        it.checkPictureBitmap {
                            val bitmap = it.getTotalLayoutBitmap()
                            if(bitmap != null ){
                                dismiss()
                                WXAccount.instance.sharePicture(bitmap, SendMessageToWX.Req.WXSceneSession)
                            }  else {
                                val msg = GlobalContextManager.getString(R.string.share_pic_create_fail)
                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
                            }
                        }
                    }
                }
                GlobalContextManager.getString(R.string.share_wechat_moments) -> {
                    sharePicCache?.let {
                        it.checkPictureBitmap {
                            val bitmap = it.getTotalLayoutBitmap()
                            if(bitmap != null ){
                                dismiss()
                                WXAccount.instance.sharePicture(bitmap, SendMessageToWX.Req.WXSceneTimeline)
                            } else {
                                val msg = GlobalContextManager.getString(R.string.share_pic_create_fail)
                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
                            }
                        }
                    }
                }
                GlobalContextManager.getString(R.string.share_save) -> {
                    sharePicCache?.let {
                        it.onSavePicToLocal(source) {
                            if(isShowing){
                                dismiss()
                            }
                        }
                    }
                }
            }
        }
        binding.rvShareActionList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        binding.rvShareActionList.adapter = shareActionAdapter

        val actions = mutableListOf<ActionItem>()
        if (WXAccount.instance.isWxInstalled()) {
            actions.add(
                ActionItem(
                    R.drawable.icon_share_wechat,
                    GlobalContextManager.getString(R.string.share_wechat)
                )
            )
            actions.add(
                ActionItem(
                    R.drawable.icon_share_moments,
                    GlobalContextManager.getString(R.string.share_wechat_moments)
                )
            )
        }
        actions.add(
            ActionItem(
                R.drawable.icon_share_save,
                GlobalContextManager.getString(R.string.share_save)
            )
        )

        shareActionAdapter.setData(actions)

        try {
            val view = activity.window.decorView
            view.isDrawingCacheEnabled = true
            view.buildDrawingCache()
            val b = view.drawingCache
            binding.bgBlur.background = BitmapDrawable(null, ImageUtils.fastBlur(b, 0.1F, 1F))
        }catch (e:Exception){

        }


    }


    fun setPostingData(data: PostingDetail?) : ShareDialogPictureCreate {
        if(data != null){
            sharePicCache = SharePicCacheLayoutPost(context)
            binding.flCacheContainer.addView(sharePicCache as SharePicCacheLayoutPost, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            (sharePicCache as SharePicCacheLayoutPost).setPostingData(data)
            binding.sharePicLayout.setPostingData(data)
        }
        return this
    }

    fun setPlaceData(data: PlaceDetail?): ShareDialogPictureCreate {
        if(data != null){
            sharePicCache = SharePicCacheLayoutPlace(context)
            binding.flCacheContainer.addView(sharePicCache as SharePicCacheLayoutPlace, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            (sharePicCache as SharePicCacheLayoutPlace).setPlaceData(data)
            binding.sharePicLayout.setPlaceData(data)
        }
        return this
    }

    fun setPlaceProductData(data: PlaceProduct?): ShareDialogPictureCreate {
        if(data != null){
            sharePicCache = SharePicCacheLayoutProduct(context)
            binding.flCacheContainer.addView(sharePicCache as SharePicCacheLayoutProduct, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            (sharePicCache as SharePicCacheLayoutProduct).setPlaceProductData(data)
            binding.sharePicLayout.setPlaceProductData(data)
        }
        return this
    }

    fun setActivityData(data: ActivitiesInfo?): ShareDialogPictureCreate {
        if(data != null){
            sharePicCache = SharePicCacheLayoutActivity(context)
            binding.flCacheContainer.addView(sharePicCache as SharePicCacheLayoutActivity, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            (sharePicCache as SharePicCacheLayoutActivity).setActivityData(data)
            binding.sharePicLayout.setActivityData(data)
        }
        return this
    }

    fun setCampLifeData(data: CampLifeInfo?): ShareDialogPictureCreate {
        if(data != null){
            sharePicCache = SharePicCacheLayoutCampLife(context)
            binding.flCacheContainer.addView(sharePicCache as SharePicCacheLayoutCampLife, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            (sharePicCache as SharePicCacheLayoutCampLife).setCampLifeData(data)
            binding.sharePicLayout.setCampLifeData(data)
            binding.container.setBackgroundColor(Color.parseColor("#C0000000"))
        }
        return this
    }

    fun setCouponDate(data: CouponInfo?): ShareDialogPictureCreate {
        if(data != null){
            sharePicCache = SharePicCacheLayoutCoupon(context)
            binding.flCacheContainer.addView(sharePicCache as SharePicCacheLayoutCoupon, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            (sharePicCache as SharePicCacheLayoutCoupon).setCouponData(data)
            binding.sharePicLayout.setCouponData(data)
            binding.container.setBackgroundColor(Color.parseColor("#C0000000"))
        }
        return this
    }


    class ActionAdapter() : RecyclerView.Adapter<BindingViewHolder<ItemDialogShareCreatePictureActionBinding>>() {

        private val dataList: MutableList<ActionItem> = mutableListOf()
        fun setData(list: List<ActionItem>) {
            dataList.clear()
            dataList.addAll(list)
            notifyDataSetChanged()
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        var actionCallBack : ((action:String)->Unit)? = null

        fun setActionCallback(callback : (action:String)->Unit) {
            actionCallBack = callback
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemDialogShareCreatePictureActionBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemDialogShareCreatePictureActionBinding>,
            position: Int
        ) {
            val item = dataList[position]
            holder.binding.icon.setImageResource(item.iconRes)
            holder.binding.text.text = item.text
            holder.itemView.click {
                actionCallBack?.invoke(item.text)
            }
        }
    }

    override fun initHeight(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initWidth(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initGravity(): Int {
        return Gravity.BOTTOM or Gravity.CENTER
    }

}